const { Service } = require("egg");

class BaseService extends Service {
	// 查询列表
	async list(pageNum, pageSize, where) {
		const data = await this.app.mysql.select(this.table, {
			// where,
			orders: [["id", "asc"]],
			limit: pageSize,
			offset: (pageNum - 1) * pageSize
		});

		// 查询总数
		const total = await this.app.mysql.count(this.table, where);
		return {
			data,
			total,
			pageNum,
			pageSize
		};
	}

	async create(data) {
		const res = await this.app.mysql.insert(this.table, data);
		// res.affectedRows(影响的行数)>0代表成功
		return res.affectedRows > 0;
	}

	async update(data) {
		const res = await this.app.mysql.update(this.table, data);
		return res.affectedRows > 0;
	}

	async destroy(id) {
		const res = await this.app.mysql.delete(this.table, { id });
		return res.affectedRows > 0;
	}
}

module.exports = BaseService;
